package com.logger.export.csv;

import java.io.File;
import java.util.HashMap;

import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;



import com.logger.LogFile;
import com.logger.LogFiles;


public final class Main {
    @Autowired
   

    /**
     * @param args
     */
    public static void main(final String[] args) {
        final String directoryPath = "C:\\Users\\bbarhoumi\\Desktop\\stageOmar\\2013-08-27\\node2";
        final String exportPath = "D:\\Export\\CSV";
        final File[] listFiles = LogFiles.listFiles(directoryPath);
        final HashMap<String, CsvExport> map = new HashMap<String, CsvExport>();
        for (final File file : listFiles) {
            final String parentFolder = LogFile.getParentFolder(file);
            final CsvExport csvExport;
            if (map.containsKey(parentFolder)) {
                csvExport = map.get(parentFolder);
            } else {
                csvExport = new CsvExport(exportPath + "\\" + parentFolder + ".csv");
                map.put(parentFolder, csvExport);
            }
            csvExport.getFiles().add(file);
        }    
        
        
        
        
        for (final CsvExport csvExport : map.values()) {
            final LogFiles logFiles = new LogFiles();
            try {
                final File[] _files = FileUtils.convertFileCollectionToFileArray(csvExport.getFiles());
                logFiles.loadUserFiles(_files);
                csvExport.writeAllFiles(logFiles);
            } catch (final Exception e) {
                e.printStackTrace();
            } finally {
                csvExport.close();
            }

        }

    }

}
